package com.jiutong.meipenggang.ui.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.entity.LoginBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.StringUtil;
import com.jiutong.meipenggang.views.GlideCircleTransform;
import com.jiutong.meipenggang.views.RoundImageView;


import butterknife.Bind;
import butterknife.OnClick;

public class LoginedActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.iv_logined_cancel)
    ImageView ivLoginedCancel;
    @Bind(R.id.tv_logined_user)
    TextView tvLoginedUser;
    @Bind(R.id.tv_logined_register)
    TextView tvLoginedRegister;
    @Bind(R.id.iv_logined_pwd)
    ImageView ivLoginedPwd;
    @Bind(R.id.et_logined_pwd)
    EditText etLoginedPwd;
    @Bind(R.id.view_login1)
    View viewLogin1;
    @Bind(R.id.tv_forget_password_login)
    TextView tvForgetPasswordLogin;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.tv_change_user)
    TextView tvChangeUser;
    @Bind(R.id.iv_logined)
    RoundImageView ivLogined;
    private UserInfoPresent userInfoPresent;
    private Bundle bundle;
    private String imgPath;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_logined;
    }

    @Override
    protected void initView() {
        super.initView();
        imgPath = SpConfig.getInstance().getString(Constants.IMAGE_PATH);
        if (!TextUtils.isEmpty(imgPath)) {
            Glide.with(this).load(imgPath).transform(new CenterCrop(this), new GlideCircleTransform(this))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade().into(ivLogined);
        } else {
            ivLogined.setImageResource(R.mipmap.icon_personal);
        }

    }

    //EditText监听事件
    private void etChangeListener() {
        etLoginedPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    ivLoginedPwd.setBackgroundResource(R.mipmap.ic_lock_pre);
                    viewLogin1.setBackgroundResource(R.color.blue_btn);
                } else {
                    ivLoginedPwd.setBackgroundResource(R.mipmap.ic_lock);
                    viewLogin1.setBackgroundResource(R.color.default_gray);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        bundle = getIntent().getExtras();
        if (!TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SHOU_SHI)) && (bundle == null || !bundle.getBoolean("forget"))) {
            openActivity(GestureVerifyActivity.class);
            AppManager.getAppManager().finishActivity(this);
        } else if (!TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.USERNAME_STRING))) {
            String phone = SpConfig.getInstance().getString(Constants.USERNAME_STRING);
            tvLoginedUser.setText(NumUilt.phoneFormat(phone));
        }
        //EditText监听事件
        etChangeListener();
        userInfoPresent = new UserInfoPresent(this, this);

    }


    @OnClick({R.id.iv_logined_cancel, R.id.btn_login, R.id.tv_change_user, R.id.tv_logined_register, R.id.tv_forget_password_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_logined_cancel:
//                openActivity(MainActivity.class);
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.btn_login:
                if (isRightAction()) {
                    userInfoPresent.login();
                }
                break;
            case R.id.tv_logined_register:
                openActivity(RegisterActivity.class);
                finish();
                break;
            case R.id.tv_change_user:
                openActivity(LoginActivity.class);
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.tv_forget_password_login:
                openActivity(PwdFrogetActivity.class);
                finish();
                break;
            default:
                break;
        }
    }

    private boolean isRightAction() {

        if (TextUtils.isEmpty(getOriPwd())) {
            toastShortShow("请输入密码");
            return false;
        }

        if (!StringUtil.isRightPwd(getOriPwd())) {
            toastShortShow("请输入6-16位数字、字母组合的密码");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return SpConfig.getInstance().getString(Constants.USERNAME_STRING);
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return etLoginedPwd.getText().toString().trim();
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        LoginBean respond = (LoginBean) object;
        if (respond.getError()==-1) {
            toastShortShow(respond.getMsg());
            SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.USERNAME_STRING, getPhoneNum()).commit();
            SpConfig.getInstance().edit().putString(Constants.ID,respond.getId()).commit();
            SpConfig.getInstance().edit().putString(Constants.TOKEN,respond.getToken()).commit();
            SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN,true).commit();
            SpConfig.getInstance().edit().putString(Constants.GESTURE_CIPHER,respond.getGestureCipher()).commit();
            SpConfig.getInstance().edit().putInt(Constants.USER_ID,respond.getUserId()).commit();
            SpConfig.getInstance().edit().putBoolean(Constants.TOKEN_FAIL,false).commit();
            openActivity(GestureEditActivity.class);
            finish();
        } else {
            toastShortShow(respond.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 4;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            openActivity(MainActivity.class);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
